package com.tianqu.tms.dao;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.tianqu.tms.core.exception.TmsDaoException;
import com.tianqu.tms.dto.IntegralRegisterDto;
import com.tianqu.tms.dto.IntegralcardUpdateHistoryDto;

/**
 * @author zhangxueliang
 * 积分卡变更记录表 实现
 */
public class IntegralcardUpdateHistoryDaoImp extends TmsDao implements
		IntegralcardUpdateHistoryDao {

	protected IntegralcardUpdateHistoryDaoImp() throws TmsDaoException {
		super();
		// TODO Auto-generated constructor stub
	}

	@Override
	public void insertHistory(IntegralcardUpdateHistoryDto dto)
			throws TmsDaoException {
		PreparedStatement ps = null;
		ResultSet rs = null;
		
		try {
			StringBuffer sql = new StringBuffer("INSERT INTO ");
			sql.append(TmsTableNames.TMS_INTEGRALCARD_UPDATE_HISTORY);
			sql.append("(");
			sql.append("memberId,");
			sql.append("memberType,");
			sql.append("businessType,");
			sql.append("accessCardIdOld,");
			sql.append("accessCardNoOld,");
			sql.append("accessCardIdNew,");
			sql.append("accessCardNoNew,");
			sql.append("changePrice,");
			sql.append("remarks,");
			sql.append("insertTime,");
			sql.append("insertUserId");
			sql.append("  ) VALUES ( ");
			sql.append("?,?,?,?,?,?,?,?,?,SYSDATE(),?)");
			ps = conn.prepareStatement(sql.toString());
			
			ps.setString(1, dto.getMemberId());
			ps.setInt(2, dto.getMemberType());
			ps.setInt(3, dto.getBusinessType());
			ps.setString(4, dto.getAccessCardIdOld());
			ps.setString(5, dto.getAccessCardNoOld());
			ps.setString(6, dto.getAccessCardIdNew());
			ps.setString(7, dto.getAccessCardNoNew());
			ps.setInt(8, dto.getChangePrice());
			ps.setString(9, dto.getRemarks());
			ps.setInt(10, dto.getInsertUserId());
			
			ps.executeUpdate();
		} catch (SQLException e) {
			throw new TmsDaoException(e.toString());
		} finally {
			close(rs, ps);
		}
	}

	@Override
	public List<IntegralcardUpdateHistoryDto> findIntegralHistory(String memId)
			throws TmsDaoException {
		PreparedStatement ps = null;
		ResultSet rs = null;
		
		
		List<IntegralcardUpdateHistoryDto> integralHistoryList = new ArrayList<IntegralcardUpdateHistoryDto>();
		StringBuffer sql = new StringBuffer("");
		sql.append("SELECT ");
		sql.append("BUSINESSTYPE,");
		sql.append("ACCESSCARDIDOLD,");
		sql.append("ACCESSCARDNOOLD,");
		sql.append("ACCESSCARDIDNEW,");
		sql.append("ACCESSCARDNONEW,");
		sql.append("REMARKS,");
		sql.append("INSERTTIME,");
		sql.append("INSERTUSERID");
		sql.append(" FROM ");
		sql.append(TmsTableNames.TMS_INTEGRALCARD_UPDATE_HISTORY);
		sql.append(" WHERE ");
		sql.append("MEMBERID=?");
		sql.append(" ORDER BY INSERTTIME DESC");
		try {
			ps = conn.prepareStatement(sql.toString());
			ps.setString(1, memId);
			
			rs = ps.executeQuery();
			while(rs.next()){
				IntegralcardUpdateHistoryDto dto = new IntegralcardUpdateHistoryDto();
				dto.setBusinessType(rs.getInt("BUSINESSTYPE"));
				dto.setAccessCardIdOld(rs.getString("ACCESSCARDIDOLD"));
				dto.setAccessCardIdNew(rs.getString("ACCESSCARDIDNEW"));
				dto.setAccessCardNoNew(rs.getString("ACCESSCARDNONEW"));
				dto.setAccessCardNoOld(rs.getString("ACCESSCARDNOOLD"));
				dto.setRemarks(rs.getString("REMARKS"));
				dto.setInsertTime(rs.getTimestamp("INSERTTIME"));
				dto.setInsertUserId(rs.getInt("INSERTUSERID"));
				integralHistoryList.add(dto);
			}
		} catch (SQLException e) {
			throw new TmsDaoException(e.toString());
		} finally {
			close(rs, ps);
		}
		
		return integralHistoryList;
	}

}
